using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MySql.Data.MySqlClient;
namespace LdExpress
{
    public class DiaryEachMeeting
    {
        public long ID = -1;
        public string Description = "";
        public string MeetingPlace = "";
        public long CustomerID = 0;
        public long UserID = 0;
        public DateTime LastUpdate = DateTime.Now;

        public DiaryEachMeeting()
        {
        }

        public DiaryEachMeeting(long lID)
        {
            this.ID = lID;
        }

        public void Save(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            string strTableName = "tbl_DiaryEachMeeting";

            if (ID <= 0) // new object, we insert new record to database
            {
                strSQL = "Insert into " + strTableName + " (" +
                            "`Description`" +
                            ",`MeetingPlace`" +
                            ",`LastUpdate`" +
                            ",`CustomerID`" +
                            ",`UserID`" +
                            ") " +
                         " values(" +
                             "'" + DBModule.RefineString(Description) + "'" +
                             ",'" + DBModule.RefineString(MeetingPlace) + "'" +
                             "," + DBModule.RefineDatetime(LastUpdate, true) +
                             "," + CustomerID.ToString() +
                             "," + UserID.ToString() +
                             ")";
                // run SQL statement
                //throw new Exception(strSQL);
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("pic", "add", cn, trans);                
            }
            else // edit object
            {
                // build SQL statement
                strSQL = "Update " + strTableName + " set " +
                            "Description='" + DBModule.RefineString(Description) + "'" +
                            ",MeetingPlace='" + DBModule.RefineString(MeetingPlace) + "'" +
                            ",LastUpdate=" + DBModule.RefineDatetime(LastUpdate, true) + "" +
                            ",CustomerID = " + CustomerID.ToString() +
                            ",UserID = " + UserID.ToString() +
                    " where ID=" + ID.ToString();
                // run SQL statement
                DBModule.ExecuteNonQuery(strSQL, cn, trans);
                UserObjectsLog.SaveObjectActionLog("pic", "edit", cn, trans);                
            }
        }
        public void Delete(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_DiaryEachMeeting WHERE ID = " + this.ID.ToString();
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("pic", "delete", cn, trans);
        }
        public void Delete(long CustomerID, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "DELETE FROM tbl_DiaryEachMeeting WHERE CustomerID = " + CustomerID;
            DBModule.ExecuteNonQuery(strSQL, cn, trans);
            UserObjectsLog.SaveObjectActionLog("pic", "delete", cn, trans);
        }
        public void Load(MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "SELECT * FROM tbl_DiaryEachMeeting WHERE ID = " + ID.ToString() + " limit 0,1";
            DataSet ds = DBModule.ExecuteQuery(strSQL, cn, trans);
            //Fill data into this object
            if (ds.Tables[0].Rows.Count > 0)
            {
                DataRow dr = ds.Tables[0].Rows[0];
                if (!dr.IsNull("ID")) ID = long.Parse(dr["ID"].ToString());
                if (!dr.IsNull("Description")) Description = dr["Description"].ToString();
                if (!dr.IsNull("MeetingPlace")) MeetingPlace = dr["MeetingPlace"].ToString();
                if (!dr.IsNull("LastUpdate")) LastUpdate = DateTime.Parse(dr["LastUpdate"].ToString());
                if (!dr.IsNull("CustomerID")) CustomerID = long.Parse(dr["CustomerID"].ToString());
                if (!dr.IsNull("UserID")) UserID = long.Parse(dr["UserID"].ToString());
            }
        }
        public static void GetList(string strCondition, string OrderBy, int NumOfItemsPerPage, int PageNo, out int NumberOfPages, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            NumberOfPages = 0;
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_DiaryEachMeeting where 1=1 ";
            if (strCondition.Length > 0)
            {
                strSQL += strCondition;
            }
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL += " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            if (NumOfItemsPerPage > 0)
            {
                ds = DBModule.ExecuteQuery(strSQL, NumOfItemsPerPage, PageNo, out NumberOfPages, cn, trans);
            }
            else
                ds = DBModule.ExecuteQuery(strSQL, cn, trans);

        }

        public void DoSearch(string strCondition, string OrderBy, out DataSet ds, MySqlConnection cn, MySqlTransaction trans)
        {
            string strSQL = "";
            ds = null;
            // build SQL statement
            strSQL = "Select * from tbl_customerofferprices where 1=1 ";
            if (strCondition.Length > 0)
                strSQL += strCondition;
            if ((OrderBy != null) && (OrderBy != ""))
                strSQL = strSQL + " Order By " + DBModule.RefineString(OrderBy);
            // run SQL statement
            ds = DBModule.ExecuteQuery(strSQL, cn, trans);
        }
    }
}